<?php
require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_INS_SHOP.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/Class_PWD.php' );
require_once( '../system/Class_MAIL.php' );
require_once( '../system/login.inc.php' );
//折込会社の場合、折込会社の自社です。
$isG4  = FALSE;
if   ($login['staff_auth'] == 'G-4') {
	$isG4 = TRUE;
}

$isG5  = FALSE;
if   ($login['staff_auth'] == 'G-5') {
	$isG5 = TRUE;
}
if (!$isG4) {

	Class_PWD::valid_auth(AOKI_AUTH_INS_SHOP,$login['staff_auth']);
} else {
	if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
		if   (!( isset($_GET['id']) && $_GET['id'] != '' )){
			echo "ペラメータエラー";
			exit;
		}
	}
}
$dao = new Class_M_INS_SHOP;

$pattern  = Class_M_OTHER::get_pattern_array(NULL, '23' );

//$selectarr = array(""=>PLEASE_SELECT_ARR, 0=>NONE_SELECT_ARR);
//$patterns = array_merge($selectarr,$pattern);

$patterns[0]  =  NONE_SELECT_ARR;
foreach ($pattern as $k =>$v){
	$patterns[$k] = $v;
}

//$selectarr = array( 0=>NONE_SELECT_ARR);
//$patterns = array_merge($selectarr,$pattern);


if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	$phase = 'input';
	if( isset($_GET['id']) && $_GET['id'] != '' ){
		$forms  = $dao->get( $_GET['id'] );
		$forms["id"] = $_GET['id'];
		//$forms["pattern_name"] = $patterns[ $forms['area_pattern_cd'] ];
		
		$forms["stop_date"] = Class_PWD::int8_to_date($forms["stop_date"]) ;
	}else{
		//$forms['auth'] = 0;
	}
}else{
	$forms = $_POST;
	if( $forms['mode'] == 'input' ){
		$err     = new Class_ERROR;
		$chk     = $dao->get_checks();
			$err_mes['ins_shop_cd']        = $err->check( $forms['ins_shop_cd'], $chk['ins_shop_cd'] );
			$err_mes['block_cd']           = $err->check( $forms['block_cd'], $chk['block_cd'] );
			$err_mes['local_cd']           = $err->check( $forms['local_cd'], $chk['local_cd'] );
			
			
			$err_mes['media_name']         = $err->check( $forms['media_name'], $chk['media_name'] );
			if (!$isG5) {
				$err_mes['ins_co_cd']          = $err->check( $forms['ins_co_cd'], $chk['ins_co_cd'] );
				$err_mes['delive_name']        = $err->check( $forms['delive_name'], $chk['delive_name'] );
				$err_mes['all_amount']         = $err->check( $forms['all_amount'], $chk['all_amount'] );
			}
			
			if (!$isG4) {
				$err_mes['area_pattern_cd']        = $err->check( $forms['area_pattern_cd'], array("NUM") );
				$err_mes['area_adnums']            = $err->check( $forms['area_adnums'], $chk['area_adnums'] );
			}
			
			if ( (!$isG4) && (!$isG5)) {
				$err_mes['shop_away']          = $err->check( $forms['shop_away'], $chk['shop_away'] );
				$err_mes['home_percent']       = $err->check( $forms['home_percent'], $chk['home_percent'] );
				
				$err_mes['sale_share']         = $err->check( $forms['sale_share'], $chk['sale_share'] );
			
				$err_mes['insert_conums']          = $err->check( $forms['insert_conums'], $chk['insert_conums'] );
				
		
				
				$err_mes['ins_shop_name']      = $err->check( $forms['ins_shop_name'], $chk['ins_shop_name'] );
				$err_mes['stop_date']          = $err->check( $forms['stop_date'], $chk['stop_date'] );
			}
		

		$whereins_shop_cd  = $dao->quote( $forms['ins_shop_cd'] );
		if( $err_mes['ins_shop_cd'] == '' ){
			if( $forms['id'] == '' ){
				$where = sprintf(" ins_shop_cd = '%s'",$whereins_shop_cd );
			}else{
				$where = sprintf(" ins_shop_cd = '%s' AND id <> %s",$whereins_shop_cd ,  $forms['id'] );
			}
			if ( $dao->exits($where) ) {
				$err->clear = false;
				$err_mes['ins_shop_cd'] = '<p class="error">このIDは重複しています</p>';
			}
		}
		
		if ( $err->clear ){
			if (!$isG5) {
				$values["ins_co_cd"]       =  $forms['ins_co_cd'] ;
				$values["ins_co_name"]     =  $forms['ins_co_name'] ;
				$values["ins_area_name"]   =  $forms['ins_area_name'] ;
				$values["delive_name"]     =  $forms['delive_name'] ;
				$values["delive_cd"]       =  $forms['delive_cd'] ; 
				$values["insert_conums"]   =  $forms['insert_conums'] ;
				$values["stop_date"]       =  Class_PWD::date_to_int8 ($forms['stop_date']) ;
				$values["memo"]            =  $forms['memo'] ;
			}
			
			
			$values["block_cd"]        =  $forms['block_cd'] ;
			$values["block_name"]      =  $forms['block_name'] ;
			
			
			$values["local_cd"]        =  $forms['local_cd'] ;
			$values["local_name"]      =  $forms['local_name'] ;
			if( $forms['id'] == '' ){
				$values["ins_shop_cd"]     =  $forms['ins_shop_cd'] ;
			}
			$values["media_cd"]        =  class_M_OTHER::do_media($values["media_name"]) ;
			$values["media_name"]      =  $forms['media_name'] ;
			if ((!$isG5)&&(!$isG4))  {
				$values["shop_away"]       =  empty( $forms['shop_away'] ) ? "0" :  $forms['shop_away'] ;
				$values["aoki_compete"]    =  $forms['aoki_compete'] ;
				$values["home_percent"]    =  empty( $forms['home_percent']  ) ? "0" :  $forms['home_percent'] ;
				$values["all_amount"]      =  empty( $forms['all_amount']  ) ? "0" :  $forms['all_amount'] ;
				$values["sale_share"]      =  empty( $forms['sale_share']  ) ? "0" :  $forms['sale_share'] ;
				
				
			}
			
			if (!$isG4) {
				$values["area_adnums"]         = empty( $forms['area_adnums'] )  ? "0" :  $forms['area_adnums'] ;
				$values["area_pattern_cd"]     = empty($forms['area_pattern_cd'])? "0" :  $forms['area_pattern_cd'] ;
				$values["area_memo"]           = $forms['area_memo'] ;
			}
			
			$values["ins_shop_name"]   =  $forms['ins_shop_name'] ;
			
			//桁数チェック
			$maxlens = $dao->get_maxlens();
			foreach ( $maxlens as $k => $v ) {
				$err_mes[$k] =$err->check_size($values[$k], $v);
			}
		}
		
		if( $err->clear ){
			if( $forms['id'] == '' ){
				if ($dao->add0($values)) {
					$forms['id'] = $id;
					$smarty->assign('message', '登録が完了しました。');
				} else {
					$smarty->assign('message', '登録が失敗しました。');
				}
			}else{
				$oldvalues = $dao->get( $forms['id'] );
				if ($dao->edit( $forms['id'], $values )) {
					$mailmsg = Class_MAIL::get_insshop_form_upd_text($oldvalues, $values, $login['staff_name'] );
					$mails  = class_M_OTHER::get_mails();
					if (Class_MAIL::mail_to($mailmsg ,$mails)) {
						$smarty->assign('message', '更新が完了しました。');
					}else {
						$smarty->assign('message', '更新が完了しました。通知メール送信失敗しました。');
					}
				} else {
					$smarty->assign('message', '更新が失敗しました。');
				}
				$id = $forms['id'];
			}
			$phase = 'complete';
			
		}else{
			
			$phase = 'input';
		}
	}
}


$ins_co = Class_M_OTHER::get_ins_co_array(PLEASE_SELECT_ARR);
if ($isG4){
	 $forms["ins_co_cd"] = $_SESSION['ADMIN_LOGIN']['STAFF_INS_CO_CD'] ;
	 $forms["ins_co_name"]  = $ins_co[$forms["ins_co_cd"]];
}

$smarty->assign('isG4', $isG4 );
$smarty->assign('isG5', $isG5 );
if( isset( $forms ) ) $smarty->assign('forms', $forms);
if( isset( $err_mes ) ) $smarty->assign('err', $err_mes);


$aoki_compete[""] =PLEASE_SELECT_ARR;
//$aoki_compete = array_merge($select, $aoki_compete_sort);
foreach ($aoki_compete_sort as $k => $v) {
	$aoki_compete[$k] = $v;
} 
$smarty->assign('aoki_compete', $aoki_compete );


$smarty->assign('ins_co', $ins_co );
$block = Class_M_OTHER::get_block_array(PLEASE_SELECT_ARR);
$smarty->assign('block', $block );
$local = Class_M_OTHER::get_local_array(PLEASE_SELECT_ARR);
$smarty->assign('local', $local );

$smarty->assign('pattern',  $patterns );

$smarty->assign('phase', $phase );
$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );

$smarty->display('minsshop_form.html');
?>